'
'   XC Engine (project Minotaur)
'   Copyright (C) 2009 Stanislav Hordiyenko
'
'   This program is free software; you can redistribute it and/or modify
'   it under the terms of the GNU General Public License as published by
'   the Free Software Foundation; either version 3 of the License, or
'   (at your option) any later version.
'
'   This program is distributed in the hope that it will be useful,
'   but WITHOUT ANY WARRANTY; without even the implied warranty of
'   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'   GNU General Public License for more details.
'
'   You should have received a copy of the GNU General Public License
'   along with this program; if not, see http://www.gnu.org/licenses
'   or write to the Free Software Foundation,Inc., 51 Franklin Street,
'   Fifth Floor, Boston, MA 02110-1301 USA
'

Class DataTable
	Private objExcelApplication
	Private objWorkBook
	Private objWorkSheet
	Private intCurrentIterationLineNumber
	Private strSpecifiedParameterName

	Private Sub Class_Initialize()
		Set objExcelApplication = CreateObject("Excel.Application") 
		
		objExcelApplication.Visible = False
		objExcelApplication.DisplayAlerts = 0
		
		strSpecifiedParameterName = Empty
	End Sub
	
	Private Sub Class_Terminate()
		Set objWorkSheet = Nothing
		Set objWorkBook = Nothing
		Set objExcelApplication = Nothing
	End Sub
	
	Public Property Get RowExists
		RowExists = False
		
		If intCurrentIterationLineNumber <= RowsCount+1 Then RowExists = True
	End Property
	
	Public Property Get CurrentRow
		CurrentRow = intCurrentIterationLineNumber-1
	End Property
	
	Public Property Get RowsCount
		RowsCount = objWorkSheet.UsedRange.Rows.Count-1
	End Property
	
	Private strEnvironmentName
	
	Public Property Let Environment(ByVal strValue)
		strEnvironmentName = strValue
		strValue = "Environment (" & strValue & ")"
		
		If objWorkBook Is Nothing Then
			' Catch the error
		End If
		
		blnWorksheetExists = False
		
		For i=1 To objWorkBook.Sheets.Count
			If UCase(strValue) = UCase(objWorkBook.Sheets(i).Name) Then
				blnWorksheetExists = True
				
				Exit For
			End If
		Next
		
		If Not blnWorksheetExists Then
			Err.Raise 5000, "DataTable->Environment", "There is no """ & strValue & """ worksheet"
		End If
		
		Set objWorkSheet = objWorkBook.WorkSheets(strValue)
	End Property

	Public Property Get Value
		If strSpecifiedParameterName = Empty Then
			Err.Raise 5000, "DataTable->Value", "Item parameter is empty"
		End If
	
		intColumnIndex = GetColumnIndex(strSpecifiedParameterName)
	
		If intColumnIndex = -1 Then
			Err.Raise 5000, "DataTable->Value", "There is no """ & strSpecifiedParameterName & """ column"
		End If
		
		Value = Trim(objExcelApplication.Cells(intCurrentIterationLineNumber, intColumnIndex).Value)
	End Property
	
	Public Sub NextRow()
		If intCurrentIterationLineNumber <= RowsCount+1 Then intCurrentIterationLineNumber = intCurrentIterationLineNumber+1
	End Sub
	
	Public Sub FirstRow()
		intCurrentIterationLineNumber = 2
	End Sub
	
	Public Sub GotoRow(ByVal intRowIndex)
		If intRowIndex > RowsCount Then
			Err.Raise 5000, "DataTable->GotoRow", "Row index is outbound of rows range"
		End If
		
		intCurrentIterationLineNumber = intRowIndex+1
	End Sub
	
	Private Function GetColumnIndex(ByVal strParameterName)
		GetColumnIndex = -1
	
		For i=1 To objWorkSheet.UsedRange.Columns.Count
			If UCase(objExcelApplication.Cells(1, i).Value) = UCase(strParameterName) Then
				GetColumnIndex = i
				
				Exit For
			End If
		Next
	End Function
	
	Private strDtFileName
	
	Public Sub Load(ByVal strFileName)
		strDtFileName = strFileName
		
		Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
		
		If Not objFso.FileExists(strFileName) Then
			Err.Raise 5000, "DataTable->Load", "DataTable doesn`t exist"
		End If
		
		Set objWorkBook = objExcelApplication.Workbooks.Open(strFileName, False, True) 
		
		Call FirstRow()
		
		Set objFso = Nothing
	End Sub
	
	Public Sub Open()
		Call Class_Initialize()
		Call Load(strDtFileName)
		
		Environment = strEnvironmentName
	End Sub
	
	Public Sub Close()
		objWorkBook.Close()
		objExcelApplication.Application.Quit()
		
		Call Class_Terminate()
	End Sub
	
	Public Default Function Item(ByVal strParameterName)
		strSpecifiedParameterName = strParameterName
		
		Set Item = Me
	End Function
End Class